| Conditions | 6 |
| Paths | 51 |
| Total Lines | 58 |
| Lines | 58 |
| Ratio | 100 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global hexo, __dirname */ |
||
| 3 | 'use strict'; |
||
| 4 | |||
| 5 | const fs = require('hexo-fs'); |
||
| 6 | const path = require('path'); |
||
| 7 | const CryptoJS = require('crypto-js'); |
||
| 8 | const log = require('hexo-log')({ |
||
| 9 | 'debug': false, |
||
| 10 | 'silent': false, |
||
| 11 | }); |
||
| 12 | |||
| 13 | hexo.extend.filter.register('after_post_render', function encrypt (data) { |
||
| 14 | |||
| 15 | // Close the encrypt function |
||
| 16 | if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) { |
||
| 17 | |||
| 18 | return data; |
||
| 19 | |||
| 20 | } |
||
| 21 | if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template |
||
| 22 | |||
| 23 | hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html')); |
||
| 24 | |||
| 25 | } |
||
| 26 | if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info |
||
| 27 | |||
| 28 | hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>'; |
||
| 29 | |||
| 30 | } |
||
| 31 | if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message |
||
| 32 | |||
| 33 | hexo.config.encrypt.default_message = 'Please enter the password to read the blog.'; |
||
| 34 | |||
| 35 | } |
||
| 36 | if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // Wrong password |
||
| 37 | |||
| 38 | hexo.config.encrypt.default_decryption_error = 'Incorrect Password!'; |
||
| 39 | |||
| 40 | } |
||
| 41 | if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // No content |
||
| 42 | |||
| 43 | hexo.config.encrypt.default_no_content_error = 'No content to display!'; |
||
| 44 | |||
| 45 | } |
||
| 46 | |||
| 47 | if ('password' in data && data.password) { |
||
| 48 | |||
| 49 | // Use the blog's config first |
||
| 50 | log.info(`Encrypted the blog: ${ data.title.trim() }`); |
||
| 51 | |||
| 52 | // Store the origin data |
||
| 53 | data.origin = data.content; |
||
| 54 | data.encrypt = true; |
||
| 55 | |||
| 56 | if (!('abstract' in data && data.abstract)) { |
||
| 57 | |||
| 58 | data.abstract = hexo.config.encrypt.default_abstract; |
||
| 59 | |||
| 60 | } |
||
| 61 | if (!('template' in data && data.template)) { |
||
| 118 |